package com.dsbj.mp.icardservice.controller;

import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.mp.icardservice.entity.*;
import com.dsbj.mp.icardservice.model.MpUser;
import com.dsbj.mp.icardservice.service.*;
import com.dsbj.mp.icardservice.webserviceclient.dsbjpmc.WxUser;
import com.zhixintech.common.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.*;

@RestController
@RequestMapping("/rest")
public class RestQuotaController {

	@Resource
	private EmployeeService employeeService;

	@Resource
	private AdjustrestService adjustrestService;

	@Resource
	private RestquotaService restquotaService;

	@Resource
	private RestclassService restclassService;

	@RequestMapping("/getRestData")
	public ResponseMessage getRestData(HttpServletRequest request) throws ParseException {
		MpUser wxUser = (MpUser)request.getAttribute("wxUser");
		Map<String,Object> map = new HashMap<String, Object>();
		String employeeno = wxUser.getEmployeeNo();
		if(StringUtils.isNotBlank(employeeno)){
			Employee employee = employeeService.getEmployeeByNo(employeeno);
			if(employee!=null){
				List<Restclass> list_year = restclassService.findByCompanynoRestclassname(employee.getCompanyno(), "年假");
				List<Restclass> list_bing = restclassService.findByCompanynoRestclassname(employee.getCompanyno(), "有薪病假");
				List<Restclass> list_tiaoxiu = restclassService.findByCompanynoRestclassname(employee.getCompanyno(), "调休假");
				//年假额度
				if(list_year !=null  && list_year.size() > 0 && !list_year.isEmpty() ){
					List<Restquota> restquotas = restquotaService.findByYearAndEmoployeenoAndCompanynoMFlex(employeeno, employee.getCompanyno(), list_year.get(0).getRestclassno());
					Date databaseDate = employeeService.getDatabaseDate();
					String databaseDate_ = DateUtil.format(databaseDate, DateUtil.DATETIME_Y_M_D);
					Date databaseDate2 = DateUtil.parse(databaseDate_, DateUtil.DATETIME_Y_M_D);
					if(restquotas!=null){
						float njed = (float) 0 ;
						for(Restquota restquota:restquotas){
							Float preyearhours = restquota.getPreyearhours();
							if(null == preyearhours){
								preyearhours = (float) 0;
							}
							if(StringUtils.isNotBlank(restquota.getStartday())){
								Date kqDate = DateUtil.parse(restquota.getStartday(), DateUtil.DATETIME_Y_M_D);
								//系统时间  可请日期  比较  可请日期比系统时间大  直接显示0
								if(kqDate.getTime() > databaseDate2.getTime()){
									njed = (float)0;
								}else{
									njed += restquota.getYearhours()+restquota.getAdjusthours()+preyearhours-restquota.getSumhours();
								}
							}
						}
						map.put("year",njed );
					}
				}
				//有薪病假额度
				if (list_bing != null && list_bing.size() > 0 && !list_bing.isEmpty() ) {
					List<Restquota> restquotas = restquotaService.findByYearAndEmoployeenoAndCompanynoMFlex(employeeno, employee.getCompanyno(), list_bing.get(0).getRestclassno());
					if (restquotas != null) {
						float yxbj = (float) 0 ;
						for(Restquota restquota:restquotas){
							yxbj += restquota.getYearhours() + restquota.getAdjusthours() - restquota.getSumhours();
						}
						map.put("bing",yxbj);
					}
				}
				//调休假额度
				if (list_tiaoxiu != null && list_tiaoxiu.size() > 0 && !list_tiaoxiu.isEmpty())  {
					List<Adjustrest> list_adjustt = adjustrestService.findByCompanynoEmployeenoYearMonth(employee.getCompanyno(), employeeno);
					if(list_adjustt!=null){
						float restadjust = (float) 0 ;
						for(Adjustrest adrest:list_adjustt){
							restadjust += adrest.getAdjusthours() + adrest.getStandardot1hours() +adrest.getStandardot2hours() - adrest.getUsedot1hours() - adrest.getUsedot2hours();
						}
						map.put("restadjust", restadjust);
					}
				}
				map.put("employee",employee);
			}
		}
		return ResponseMessage.ok(map);
	}
	/**
	 *
	 * @param
	 * @return
	 * @author wcm 2019-12-09
	 */
	public static void main(String[] args){

	}

}
